package 
{
  import com.bit101.components.PushButton;
  import com.bit101.components.Style;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  
	/**
	 * TODO remove ' from dic.
	 * @author ivan
	 */
	public class Anagrams extends Sprite 
	{
    
		public function Anagrams():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
      
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      stage.stageFocusRect = false;
      
      graphics.beginFill(0xFFFFFF);
      graphics.drawRect(0, 0, 424, 300);
      graphics.endFill();
      
      Style.fontSize = 8;
      startButton = new PushButton(this, stage.width/2-50, stage.height/2-20, "START!", startRound);
      startButton.height = 40;
      startButton.draw();
      
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      preloadGameBoard = 1;
		}
    
    private function startRound(e:Event = null):void
    {
      if (startButton.parent)
        removeChild(startButton);
      if (!gameBoard)
        gameBoard = new GameBoard(showResume);
      addChild(gameBoard);
      gameBoard.start();
    }
    
    private function showResume(result:PlayerResult):void
    {
      gameBoard.removeAllListeners();
      removeChild(gameBoard);
      
      scoreBoard = new ScoreBoard(result, restart);
      addChild(scoreBoard);
      
      preloadGameBoard = 2;
    }
    
    private function restart(e:Event):void
    {
      removeChild(scoreBoard);
      startRound();
    }
    
    private function onEnterFrame(e:Event):void
    {
      if (--preloadGameBoard == 0)
      {
        gameBoard = new GameBoard(showResume);
      }
      if (preloadGameBoard < -1) preloadGameBoard = -1;
    }
		
    private var startButton:PushButton;
    private var gameBoard:GameBoard;
    private var scoreBoard:ScoreBoard;
    private var preloadGameBoard:int;
	}
	
}